package com.light;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.light.model.Vocabulary;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private Button bt_search;
    private EditText et_land,et_taiwan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();

//        Vocabulary mVocabulary = new Vocabulary();
//        mVocabulary.setType("common");
//        mVocabulary.setLandword("本科生");
//        mVocabulary.setTaiwanword("大学生");
//
//        mVocabulary.save(new SaveListener<String>() {
//            @Override
//            public void done(String objectId,BmobException e) {
//                if(e==null){
//                    Toast.makeText(MainActivity.this, "添加数据成功，返回objectId为:"+objectId, Toast.LENGTH_SHORT).show();
//                }else{
//                    Toast.makeText(MainActivity.this, "创建数据失败,"+e.getMessage(), Toast.LENGTH_SHORT).show();
//                }
//            }
//        });



    }

    private void initUI() {
        bt_search= (Button) findViewById(R.id.bt_search);
        bt_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                BmobQuery<Vocabulary> bmobQuery = new BmobQuery<Vocabulary>();
                bmobQuery.addWhereEqualTo("landword",et_land.getText().toString());
                bmobQuery.findObjects(new FindListener<Vocabulary>() {
                    @Override
                    public void done(List<Vocabulary> list, BmobException e) {
                        if(e==null){
                            Toast.makeText(MainActivity.this, "查询成功：共"+list.size()+"条数据。", Toast.LENGTH_SHORT).show();
                            for (Vocabulary obj : list) {
                                //获得playerName的信息
                                String word = obj.getTaiwanword();
                                Log.d(TAG,"台湾词语："+word);
                                et_taiwan.setText(word);
                            }
                        }else{
                            Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }
        });
        et_land= (EditText) findViewById(R.id.et_land);
        et_taiwan=(EditText)findViewById(R.id.et_taiwan);
    }
}
